package com.tsmask.bound;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 

import javax.swing.JButton;
import javax.swing.JComboBox;

import com.alibaba.fastjson.JSONArray;
import com.tsmask.HttpUtil;
import com.tsmask.Main;
import com.tsmask.layout.MainJFrame; 

/**
 * 城市栏组件
 * 
 * @author TsMask
 *
 */
public class CityBound {
	HttpUtil httpUtil = new HttpUtil();
	/**
	 * 省市下拉列表
	 * 
	 * @return 下拉列表组件
	 */
	public static JComboBox<String> provinceJComboBox = new JComboBox<String>();
	public JComboBox<String> province() { 
		provinceJComboBox.setBounds(12, 12, 88, 28);
		provinceJComboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 取当前省市列表
				JSONArray jsonArray = Main.provinceArray;
				JSONArray provinceArr = jsonArray.getJSONArray(provinceJComboBox.getSelectedIndex());
				// System.out.println(provinceArr);
				// 城市数据 
				httpUtil.areaQuery("city", provinceArr.getString(1));
				// 取当前城市省市列表
				CityBound.cityJComboBox.removeAllItems();
				JSONArray cityArray = Main.cityArray; 
				for(Object city : cityArray) {
					JSONArray cityArr = (JSONArray) city;
					CityBound.cityJComboBox.addItem(cityArr.getString(0));
				}
				CityBound.cityJComboBox.setSelectedItem(cityArray.getJSONArray(0).getString(0));
				// -----------有重复代码片段，需要提取-------------
				// 取当前省市列表 
				JSONArray cityArr = Main.cityArray.getJSONArray(cityJComboBox.getSelectedIndex());
				// 城市数据 
				httpUtil.areaQuery("town", cityArr.getString(1));
				// 取当前城市省市列表
				CityBound.townJComboBox.removeAllItems();
				JSONArray townArray = Main.townArray; 
				for(Object town : townArray) {
					JSONArray townArr = (JSONArray) town;
					CityBound.townJComboBox.addItem(townArr.getString(0));
				}
				CityBound.townJComboBox.setSelectedItem(townArray.getJSONArray(0).getString(0));
			}
		});
		return provinceJComboBox;
	}

	/**
	 * 城市下拉列表
	 * 
	 * @return 下拉列表组件
	 */
	public static JComboBox<String>  cityJComboBox = new JComboBox<String>();
	public JComboBox<String> city() { 
		cityJComboBox.setBounds(108, 12, 88, 28);
		cityJComboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(cityJComboBox.getSelectedIndex() == -1) return;
				// 取当前省市列表
				JSONArray jsonArray = Main.cityArray;
				JSONArray cityArr = jsonArray.getJSONArray(cityJComboBox.getSelectedIndex());
				// System.out.println(cityArr);
				// 城市数据 
				httpUtil.areaQuery("town", cityArr.getString(1));
				// 取当前城市省市列表
				CityBound.townJComboBox.removeAllItems();
				JSONArray townArray = Main.townArray; 
				for(Object town : townArray) {
					JSONArray townArr = (JSONArray) town;
					CityBound.townJComboBox.addItem(townArr.getString(0));
				}
				CityBound.townJComboBox.setSelectedItem(townArray.getJSONArray(0).getString(0));
			}
		});
		return cityJComboBox;
	}

	/**
	 * 城区省市下拉列表
	 * 
	 * @return 下拉列表组件
	 */
	public static JComboBox<String>  townJComboBox = new JComboBox<String>();
	public JComboBox<String> town() {
		townJComboBox.setBounds(204, 12, 88, 28);
		return townJComboBox;
	}

	/**
	 * 更换城市
	 * 
	 * @return 按钮组件
	 */
	public JButton change() {
		JButton jButton = new JButton("更换");
		jButton.setBounds(300, 16, 60, 25);
		jButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) { 
				// 取当前省市列表
				JSONArray townArr = Main.townArray.getJSONArray(townJComboBox.getSelectedIndex());
				// System.out.println(townArr);
				// 请求天气详情
				httpUtil.weatherQuery(townArr.getString(1));
				// 刷新天气信息
				MainJFrame.seeJPanel.repaintPanel();
			}
		});
		return jButton;
	}
}
